package com.xbts.screen.controller.screen;

import com.xbts.common.core.constant.CacheConstants;
import com.xbts.common.core.constant.Constants;
import com.xbts.common.core.enums.DateTypeEnum;
import com.xbts.common.core.utils.StringUtils;
import com.xbts.common.core.web.controller.BaseController;
import com.xbts.common.core.web.domain.AjaxResult;
import com.xbts.common.core.web.domain.BaseHistogramResult;
import com.xbts.common.core.web.domain.BasePieChartResult;
import com.xbts.common.core.web.domain.BaseRankingResult;
import com.xbts.common.security.annotation.RequiresPermissions;
import com.xbts.screen.domain.vo.ScreenConsumeHistogramVO;
import com.xbts.screen.domain.vo.ScreenQualityDetailsVO;
import com.xbts.screen.service.IScreenServer;
import com.xbts.screen.service.ISyntheticScreenServer;
import com.xbts.screen.service.IWeVirtualDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 大屏-首页
 */
@Api(tags = "大屏-首页")
@RequestMapping("/home")
@RestController
public class NewScreenController extends BaseController {

    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    private IWeVirtualDataService weVirtualDataService;

    @Autowired
    private ISyntheticScreenServer syntheticScreenServer;
    /**
     * 展示虚拟数据
     */
    @ApiOperation("大屏-展示虚拟数据")
    @GetMapping("/showVirtualData")
    public AjaxResult showVirtualData(){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        return success(StringUtils.equals(showVirtualData, Constants.TRUE));
    }
    /**
     * 大屏-业务信息
     */
    @ApiOperation("大屏-业务信息")
    @RequiresPermissions("screen:business:details")
    @GetMapping("/screenBusinessDetails")
    public AjaxResult screenBusinessDetails(){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.screenBusinessDetails());
        } else {
            BaseHistogramResult result = syntheticScreenServer.screenBusinessDetails();
            if(result == null){
                return success(weVirtualDataService.screenBusinessDetails());
            }
            return success(result);
        }
    }
    /**
     * 大屏-产能信息
     */
    @ApiOperation("大屏-产能信息")
    @RequiresPermissions("screen:capacity:details")
    @GetMapping("/screenCapacityDetails")
    public AjaxResult screenCapacityDetails(){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.screenCapacityDetails());
        } else {
            BaseHistogramResult result = syntheticScreenServer.screenCapacityDetails();
            if(result == null){
                return success(weVirtualDataService.screenCapacityDetails());
            }
            return success(result);
        }
    }
    /**
     * 大屏-成本信息A
     */
    @ApiOperation("大屏-成本信息-饼状图")
    @RequiresPermissions("screen:cost:details:a")
    @GetMapping("/screenCostDetailsA")
    public AjaxResult screenCostDetailsA(){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.screenCostDetailsA());
        } else {
            List<BasePieChartResult> result = syntheticScreenServer.screenCostDetailsA(null);
            if(result == null){
                return success(weVirtualDataService.screenCostDetailsA());
            }
            return success(result);
        }
    }
    /**
     * 大屏-成本信息B
     */
    @ApiOperation("大屏-成本信息-柱状图")
    @RequiresPermissions("screen:cost:details:b")
    @GetMapping("/screenCostDetailsB")
    public AjaxResult screenCostDetailsB(){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.screenCostDetailsB());
        } else {
            BaseHistogramResult result = syntheticScreenServer.screenCostDetailsB(null);
            if(result == null){
                return success(weVirtualDataService.screenCostDetailsB());
            }
            return success(result);
        }
    }
    /**
     * 大屏-能耗信息
     */
    @ApiOperation("大屏-能耗信息")
    @RequiresPermissions("screen:consume:details")
    @GetMapping("/screenConsumeDetails")
    public AjaxResult screenConsumeDetails(){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.screenConsumeDetails());
        } else {
            ScreenConsumeHistogramVO result = (ScreenConsumeHistogramVO)redisTemplate.opsForValue().get(CacheConstants.SCREEN_CONSUME_DETAILS);
            if(result != null){
                return success(result);
            } else {
                result = syntheticScreenServer.screenConsumeDetails();
                if(result != null) {
                    redisTemplate.opsForValue().set(CacheConstants.SCREEN_CONSUME_DETAILS, result, 15, TimeUnit.MINUTES);
                }
            }
            if(result == null){
                return success(weVirtualDataService.screenConsumeDetails());
            }
            return success(result);
        }
    }
    /**
     * 大屏-品质信息
     */
    @ApiOperation("大屏-品质信息")
    @RequiresPermissions("screen:capacity:details")
    @GetMapping("/screenQualityDetails")
    public AjaxResult screenQualityDetails(){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.screenQualityDetails());
        } else {
            List<ScreenQualityDetailsVO> result = syntheticScreenServer.screenQualityDetails();
            if(result == null){
                return success(weVirtualDataService.screenQualityDetails());
            }
            return success(result);
        }
    }
    /**
     * 大屏-品质分析A
     */
    @ApiOperation("大屏-品质分析-近6个月")
    @RequiresPermissions("screen:quality:analyse:a")
    @GetMapping("/screenQualityAnalyseA")
    public AjaxResult screenQualityAnalyseA(){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.screenQualityAnalyseA());
        } else {
            BaseHistogramResult result = syntheticScreenServer.screenQualityAnalyseA();
            if(result == null){
                return success(weVirtualDataService.screenQualityAnalyseA());
            }
            return success(result);
        }
    }
    /**
     * 大屏-品质分析B
     */
    @ApiOperation("大屏-品质分析-月周天")
    @RequiresPermissions("screen:quality:analyse:b")
    @GetMapping("/screenQualityAnalyseB")
    public AjaxResult screenQualityAnalyseB(){
        String showVirtualData = (String)redisTemplate.opsForValue().get(CacheConstants.SYS_SHOW_VIRTUAL_DATA);
        if (StringUtils.equals(showVirtualData, Constants.TRUE)){
            return success(weVirtualDataService.screenQualityAnalyseB());
        } else {
            BaseHistogramResult result = syntheticScreenServer.screenQualityAnalyseB();
            if(result == null){
                return success(weVirtualDataService.screenQualityAnalyseB());
            }
            return success(result);
        }
    }

}
